package com.zhida.jiyangcenter.mian.fragment

import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import chooongg.base.annotation.ContentLayout
import chooongg.base.utils.*
import chooongg.http.APIException
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.flyco.animation.BounceEnter.BounceTopEnter
import com.flyco.animation.SlideExit.SlideBottomExit
import com.flyco.dialog.listener.OnBtnClickL
import com.flyco.dialog.widget.NormalDialog
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.APIAccount
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.api.PersonalInfoEntity
import com.zhida.jiyangcenter.api.ReferralCodeEntity
import com.zhida.jiyangcenter.coupon.MyCouponActivity
import com.zhida.jiyangcenter.membership.MembershipBuyActivity
import com.zhida.jiyangcenter.mian.BaseFragment
import com.zhida.jiyangcenter.mian.CardServeActivity
import com.zhida.jiyangcenter.modle.ComplaintActivity
import com.zhida.jiyangcenter.modle.EquipmentListActivity
import com.zhida.jiyangcenter.modle.MyCommentActivity
import com.zhida.jiyangcenter.modle.SeeEquipmentActivity
import com.zhida.jiyangcenter.setting.SettingActivity
import com.zhida.jiyangcenter.order.ServeOrderListActivity
import com.zhida.jiyangcenter.order.ShopOrderListActivity
import com.zhida.jiyangcenter.setting.UserInfoActivity
import com.zhida.jiyangcenter.utils.SP
import com.zhida.jiyangcenter.utils.glideImage
import com.zhida.jiyangcenter.utils.request
import kotlinx.android.synthetic.main.fragment_main_me.*

@ContentLayout(R.layout.fragment_main_me)
class MainMeFragment : BaseFragment() {

    override fun getTitle(): String {
        return "我的"
    }

    val adapterReport = Adapter()
    val adapterService = Adapter()
    val adapterShop = Adapter()
    val adapterProfit = Adapter()
    val adapterTool = Adapter()
    private var listener: ((Int) -> Unit)? = null
    fun setListener(listener: ((Int) -> Unit)) {
        this.listener = listener
    }

    override fun initConfig(savedInstanceState: Bundle?) {
        view_status_bar.updateLayoutParams<ConstraintLayout.LayoutParams> {
            height = getStatusBarHeight()
        }
        bg_main_me_top.setOnClickListener {
            startActivity<MembershipBuyActivity> { }
        }
        tv_main_me_card.setOnClickListener {
            startActivity<MembershipBuyActivity> {

            }
        }
        rv_main_me_report.layoutManager = object : GridLayoutManager(context, 4) {
            override fun canScrollHorizontally() = false
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rv_main_me_report.adapter = adapterReport
        adapterReport.setNewData(ArrayList<MainMeMode>().apply {
            add(MainMeMode("查看报告", R.drawable.iv_main_me_see_report))
            add(MainMeMode("管理设备", R.drawable.iv_main_me_equipment))
            add(MainMeMode("健康管家", R.drawable.iv_main_me_healthy))
            add(MainMeMode("我的收藏", R.drawable.iv_main_me_commont))
        })
        adapterReport.setOnItemClickListener { _, _, position ->
            when (adapterReport.data[position].title) {
                "我的收藏" -> {
                    startActivity<MyCommentActivity>() { }
                }
                "管理设备" -> {
                    startActivity<EquipmentListActivity> { }
                }
                "健康管家" -> {
                    listener?.invoke(1)
                }
                "查看报告" -> {
                    startActivity<SeeEquipmentActivity> { }
                }
                else -> {
                    toast("开发中,敬请期待!!")
                }
            }
        }
        niv_main_me_head.setOnClickListener {
            startActivity<UserInfoActivity> { }
        }
        tv_main_me_name.setOnClickListener {
            startActivity<UserInfoActivity> { }
        }

        iv_main_me_setting.setOnClickListener {
            startActivity<SettingActivity> { }
        }
        tv_main_me_setting.setOnClickListener {
            startActivity<SettingActivity> { }
        }
        rv_main_me_service.layoutManager = object : GridLayoutManager(context, 4) {
            override fun canScrollHorizontally() = false
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rv_main_me_service.adapter = adapterService
        adapterService.setNewData(ArrayList<MainMeMode>().apply {
            add(MainMeMode("全部服务", R.drawable.iv_main_me_all_service))
            add(MainMeMode("已使用", R.drawable.iv_main_me_service_payment))
            add(MainMeMode("待使用", R.drawable.iv_main_me_service_use))
//            add(MainMeMode("退款/售后", R.drawable.iv_main_me_service_refund))
        })
        adapterService.setOnItemClickListener { _, view, position ->
            startActivity<ServeOrderListActivity> {

            }
        }

        rv_main_me_shop.layoutManager = object : GridLayoutManager(context, 4) {
            override fun canScrollHorizontally() = false
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rv_main_me_shop.adapter = adapterShop
        adapterShop.setNewData(ArrayList<MainMeMode>().apply {
            add(MainMeMode("全部订单", R.drawable.iv_main_me_all_order))
            add(MainMeMode("待付款", R.drawable.iv_main_me_order_payment))
            add(MainMeMode("待收货", R.drawable.iv_main_me_order_receipt))
            add(MainMeMode("退款/售后", R.drawable.iv_main_me_service_refund))
        })
        adapterShop.setOnItemClickListener { _, view, position ->
            startActivity<ShopOrderListActivity> {

            }
        }

        rv_main_me_profit.layoutManager = object : GridLayoutManager(context, 4) {
            override fun canScrollHorizontally() = false
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rv_main_me_profit.adapter = adapterProfit
        adapterProfit.setNewData(ArrayList<MainMeMode>().apply {
            add(MainMeMode("推广码", R.drawable.iv_main_me_extension))
            add(MainMeMode("投诉建议", R.drawable.iv_main_me_complaint))
//            add(MainMeMode("健康豆", R.drawable.iv_main_me_jiankangdou))
//            add(MainMeMode("收益", R.drawable.iv_main_me_profit))
//            add(MainMeMode("优惠券", R.drawable.iv_main_me_coupon))
        })
        adapterProfit.setOnItemClickListener { _, view, position ->
            when (adapterProfit.data.get(position).title) {
                "优惠券" -> {
                    startActivity<MyCouponActivity> { }
                }
                "投诉建议" -> {
                    startActivity<ComplaintActivity> { }
                }
                "推广码" -> {
                    lifecycleScope.request({ APIAccount.service().referralCode() },
                        object : BaseAPI<ReferralCodeEntity>() {
                            override fun onSuccess(
                                t: ReferralCodeEntity?,
                                state: Int,
                                msg: String
                            ) {
                                val dialog = NormalDialog(context)
                                dialog.content("您的推广码是：${t?.referral_code}")
                                    .style(NormalDialog.STYLE_TWO)//
                                    .title("查看推广码")
                                    .btnNum(1)
                                    .btnTextColor(
                                        resColor(R.color.colorAccent)
                                    )
                                    .btnText("我已知晓")
                                    .titleTextSize(23f)
                                    .showAnim(BounceTopEnter())
                                    .dismissAnim(SlideBottomExit())
                                    .show()

                                dialog.setOnBtnClickL(OnBtnClickL {
                                    dialog.dismiss()
                                })
                            }

                            override fun onError(e: APIException) {
                                tipFail(e.eMessage)
                            }

                        })
                }
                else -> {
                    toast("开发中,敬请期待!!")
                }
            }
        }
        rv_main_me_tool.layoutManager = object : GridLayoutManager(context, 4) {
            override fun canScrollHorizontally() = false
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
//        rv_main_me_tool.adapter = adapterTool
//        adapterTool.setNewData(ArrayList<MainMeMode>().apply {
//            add(MainMeMode("投诉建议", R.drawable.iv_main_me_complaint))
//            add(MainMeMode("历史痕迹", R.drawable.iv_main_me_histroy))
//            add(MainMeMode("保险服务", R.drawable.iv_main_me_insurance))
//            add(MainMeMode("其他", R.drawable.iv_main_me_orher))
//        })
//        adapterTool.setOnItemClickListener { _, view, position ->
//            when (adapterTool.data.get(position).title) {
//                else -> {
//                    toast("开发中,敬请期待!!")
//                }
//            }
//        }
        riv_main_me_card.setOnClickListener {
            startActivity<MembershipBuyActivity> {

            }
        }
    }

    override fun onResume() {
        super.onResume()
        getData()
    }

    private fun getData() {
        lifecycleScope.request(
            { APIAccount.service().personalInfo() },
            object : BaseAPI<PersonalInfoEntity>() {
                override fun onSuccess(t: PersonalInfoEntity?, state: Int, msg: String) {
                    SP.avatar_path.put(t?.avatar_path)
                    SP.nickname.put(t?.nickname)
                    glideImage(niv_main_me_head, t?.avatar_path ?: "")
                    tv_main_me_name.text = t?.nickname ?: t?.phone

                    when (t?.member?.level) {
                        "1" -> {
                            tv_main_me_card.gone()
                            bg_main_me_top.inVisible()
                            glideImage(riv_main_me_card, t.card_path ?: "")
                            tv_main_card_type.text = "体验卡"
                            tv_main_card_code.text=t.card_code
                        }
                        "2" -> {
                            tv_main_me_card.gone()
                            bg_main_me_top.inVisible()
                            glideImage(riv_main_me_card, t.card_path ?: "")
                            tv_main_card_code.text=t.card_code
                            tv_main_card_type.text = "黄金卡"
                        }
                        "3" -> {
                            glideImage(riv_main_me_card, t.card_path ?: "")
                            tv_main_card_code.text=t.card_code
                            tv_main_card_type.text = "翡翠卡"
                            bg_main_me_top.inVisible()
                            tv_main_me_card.gone()
                        }
                        "4" -> {
                            glideImage(riv_main_me_card, t.card_path ?: "")
                            tv_main_card_code.text=t.card_code
                            tv_main_card_type.text = "钻石卡"
                            tv_main_me_card.gone()
                            bg_main_me_top.inVisible()
                        }
                        else -> {
                            tv_main_me_card.text = "中昊云景 年费会员  点击查看/领取会员权益"
                        }

                    }
                }

                override fun onError(e: APIException) {
                    tipFail(e.eMessage)
                }

            })
    }


    class Adapter : BaseQuickAdapter<MainMeMode, BaseViewHolder>(R.layout.item_main_me) {
        override fun convert(helper: BaseViewHolder, item: MainMeMode?) {
            helper.setImageResource(R.id.iv_icon, item?.imageResId ?: -1)
            helper.setText(R.id.textView, item?.title ?: "")

        }

    }

    class MainMeMode(val title: String, val imageResId: Int)
}